import re
import requests
import threading

def get_url(page_num):
    url='https://www.qiushibaike.com/imgrank/page/%s/'%(page_num)
    res=requests.get(url).text
    list=re.findall(r'pic.qiushibaike.com/system/pictures/.*?g',res)
    return list

def download(list):
    for i in list:
        file_name=i.split('/')[-1]
        url='http://%s'%i
        r=requests.get(url)
        with open('/Users/yinhaodong/Desktop/tupian/%s'%file_name,'wb')as f:
            f.write(r.content)
            print('%s download sucess!'%file_name)

def get_pic():
    while True:
        firstpage=int(input('从哪一页开始抓取：'))
        lastpage=int(input('从哪一页停止抓取：'))
        print('--------开始抓取图片---------')
        for i in range(firstpage,lastpage+1):
            pic_list=get_url(i)
            thread_list=[]
            downloading=threading.Thread(target=download,args=(pic_list,))
            thread_list.append(downloading)
            downloading.start()
            for i in thread_list:
                i.join()
        print('抓取完成')

        break

choice=input('这是用来抓取糗事百科图片的工具，如果需要退出请输入E:')
if choice !='E':
    get_pic()
else:
    print('欢迎下次使用')
